package com.bestcem.xm.member.grpc.service;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.member.grpc.service.param.footprint.ListFootprintRespondentSeqsParam;
import com.bestcem.xm.member.grpc.v1.services.FootprintServiceGrpc;
import com.bestcem.xm.member.grpc.v1.services.ListFootprintRespondentSeqsRequest;
import com.bestcem.xm.member.grpc.v1.services.ListFootprintRespondentSeqsResponse;
import com.bestcem.xm.member.service.FootprintService;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;

/**
 * 足迹点 grpcService
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/10/22
 */
@GrpcService
public class FootprintGrpcService extends FootprintServiceGrpc.FootprintServiceImplBase {

    @Resource
    private FootprintService footprintService;

    /**
     * 根据 组织id 获取 足迹点的答卷编号
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void listFootprintRespondentSeqs(ListFootprintRespondentSeqsRequest request,
                                            StreamObserver<ListFootprintRespondentSeqsResponse> responseObserver) {
        ListFootprintRespondentSeqsResponse.Builder builder =
                ListFootprintRespondentSeqsResponse.newBuilder();

        ListFootprintRespondentSeqsParam param = requestToListFootprintRespondentSeqsParam(request);

        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        // 获取答卷编号
        ServiceResult<List<String>> result = footprintService.listFootprintRespondentSeqs(param);
        if(!result.isSuccess()){
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        List<String> data = result.getData();
        if (CollUtil.isNotEmpty(data)) {
            builder.addAllRespondentSeqs(data);
        }
        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * request To ListFootprintRespondentSeqsParam
     *
     * @param request
     * @return
     */
    private ListFootprintRespondentSeqsParam requestToListFootprintRespondentSeqsParam(ListFootprintRespondentSeqsRequest request) {
        ListFootprintRespondentSeqsParam param = new ListFootprintRespondentSeqsParam();
        // 组织id
        param.setOrgId(request.getOrgId());
        return param;
    }
}
